﻿@page "/Grid/EditData/InputValidation"
@layout DataProviderAccessArea<INwindDataProvider>

<DemoPageSectionComponent Id="Grid-Editing-InputValidation" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @inject NwindDataService NwindDataService

        <DxGrid Data="DataSource" PageSize="12" KeyFieldName="EmployeeId"
            CustomizeDataRowEditor="Grid_CustomizeDataRowEditor" EditModelSaving="Grid_EditModelSaving"
            DataItemDeleting="Grid_DataItemDeleting" PopupEditFormCssClass="pw-800" EditMode="@EditMode.Mode"
            ColumnResizeMode="GridColumnResizeMode.NextColumn" TextWrapEnabled="false" SizeMode="Params.SizeMode"
            HighlightRowOnHover="true">
            <Columns>
                <DxGridCommandColumn Width="160px" />
                <DxGridDataColumn FieldName="FirstName" MinWidth="80" />
                <DxGridDataColumn FieldName="LastName" MinWidth="80" />
                <DxGridDataColumn FieldName="Title" MinWidth="100" />
                <DxGridDataColumn FieldName="TitleOfCourtesy" MinWidth="120" />
                <DxGridDataColumn FieldName="BirthDate" Width="10%" MinWidth="80" />
                <DxGridDataColumn FieldName="HireDate" Width="10%" MinWidth="80" />
            </Columns>
            <EditFormTemplate Context="EditFormContext">
                <DxFormLayout CssClass="w-100">
                    <DxFormLayoutItem Caption="First Name:" ColSpanMd="6">
                        @EditFormContext.GetEditor("FirstName")
                    </DxFormLayoutItem>
                    <DxFormLayoutItem Caption="Last Name:" ColSpanMd="6">
                        @EditFormContext.GetEditor("LastName")
                    </DxFormLayoutItem>
                    <DxFormLayoutItem Caption="Title:" ColSpanMd="6">
                        @EditFormContext.GetEditor("Title")
                    </DxFormLayoutItem>
                    <DxFormLayoutItem Caption="Title of Courtesy:" ColSpanMd="6">
                        @EditFormContext.GetEditor("TitleOfCourtesy")
                    </DxFormLayoutItem>
                    <DxFormLayoutItem Caption="Birth Date:" ColSpanMd="6">
                        @EditFormContext.GetEditor("BirthDate")
                    </DxFormLayoutItem>
                    <DxFormLayoutItem Caption="Hire Date:" ColSpanMd="6">
                        @EditFormContext.GetEditor("HireDate")
                    </DxFormLayoutItem>
                </DxFormLayout>
            </EditFormTemplate>
            <ToolbarTemplate>
                <DxToolbar ItemRenderStyleMode="ToolbarRenderStyleMode.Plain" SizeMode="Params.SizeMode">
                    <Items>
                        <DxToolbarItem BeginGroup="true" Alignment="ToolbarItemAlignment.Left">
                            <Template Context="toolbar_item_context">
                                <div class="d-flex flex-row align-items-center h-100">
                                    <div class="me-2">Edit Mode:</div>
                                    <DxComboBox Data="@GridEditModes" TextFieldName="Name"
                                        @bind-Value="@EditMode" />
                                </div>
                            </Template>
                        </DxToolbarItem>
                    </Items>
                </DxToolbar>
            </ToolbarTemplate>
        </DxGrid>
    </ChildContentWithParameters>

    @code {
        IEnumerable<EditableEmployee> DataSource { get; set; }
        GridEditModeItem EditMode { get; set; }
        IEnumerable<GridEditModeItem> GridEditModes { get; } = Enum.GetValues<GridEditMode>().Select(e =>
        GridEditModeItem.Create(e)).ToList();

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            DataSource = await NwindDataService.GetEmployeesEditableAsync();
            EditMode = GridEditModes.First();
        }
        void Grid_CustomizeDataRowEditor(GridCustomizeDataRowEditorEventArgs e)
        {
            if (e.EditSettings is ITextEditSettings settings)
                settings.ShowValidationIcon = true;
        }
        async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e)
        {
            var editableEmployee = (EditableEmployee)e.EditModel;

            if (e.IsNew)
                await NwindDataService.InsertEmployeeAsync(editableEmployee);
            else
                await NwindDataService.UpdateEmployeeAsync((EditableEmployee)e.DataItem, editableEmployee);
            await UpdateDataAsync();
        }
        async Task Grid_DataItemDeleting(GridDataItemDeletingEventArgs e)
        {
            await NwindDataService.RemoveEmployeeAsync((EditableEmployee)e.DataItem);
            await UpdateDataAsync();
        }
        async Task UpdateDataAsync()
        {
            DataSource = await NwindDataService.GetEmployeesEditableAsync();
        }
        record GridEditModeItem(string Name, GridEditMode Mode)
        {
            public static GridEditModeItem Create(GridEditMode mode) => new
            (SplitTextHelper.SplitPascalCaseString(mode.ToString()), mode);
        }
    }
</DemoPageSectionComponent>
